package com.gmail.jaredjeya.awesomeplatformer.model.sprites;

import java.io.IOException;

import com.gmail.jaredjeya.awesomeplatformer.Constants;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Sprite {
	
	
	protected Bitmap image;
	protected int size;
	public String name;
	
	public static Sprite blueSprite = new Sprite(1, 0xFF0000FF, "blue");
	public static Sprite redSprite = new Sprite(1, 0xFFFF0000, "red");
	public static Sprite blackSprite = new Sprite(1, 0xFF111111, "bg");
	public static Sprite playerSprite = new Sprite(1, 0xFFFFFF00, "player");
	public static Sprite greenSprite = new Sprite(1, 0xFF00FF00, "green");
	public static Sprite leftButtonSprite, rightButtonSprite, shootButtonSprite, jumpButtonSprite;
	public static Sprite leftButtonSpriteP, rightButtonSpriteP, shootButtonSpriteP, jumpButtonSpriteP;
	
	public Sprite(int size, int colour, String name) {
		createBlankSprite(size, colour, name);
	}

	/**
	 * @param size
	 * @param colour
	 * @param name
	 */
	protected void createBlankSprite(int size, int colour, String name) {
		this.size = size;
		this.name = name;
		image = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
		int pixels[] = new int[size * size];
		for(int i = 0; i < pixels.length; i++) pixels[i] = colour;
		image.setPixels(pixels, 0, size, 0, 0, size, size);
	}
	
	public Sprite(String path, Context context, String name) {
		try {
			image = BitmapFactory.decodeStream(context.getAssets().open(path));
			//context.getAssets().close();
			if (image == null) Log.v(Constants.DEBUG_TAG, "YIkes");
			size = Math.max(image.getWidth(), image.getHeight());
	
		}
		catch (IOException e) {
			createBlankSprite(8, 0xFFFFFFFF, name);
			e.printStackTrace();
		}
	}
	
	//This works, but I prefer to organise my pictures in Assets.
	public Sprite(int id, Context context, String name) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inMutable = true;
		image = BitmapFactory.decodeResource(context.getResources(), id, options);
		size = Math.max(image.getWidth(), image.getHeight());
		this.name = name;
	}
	
	public Bitmap getBitmap(int height, int width) {
		return Bitmap.createScaledBitmap(image, width, height, false);
	}
	
	public static void load(Context context) {
		leftButtonSprite = new Sprite("sprites/left.png", context, "left");
		rightButtonSprite = new Sprite("sprites/right.png", context, "right");
		shootButtonSprite = new Sprite("sprites/shoot.png", context, "shoot");
		jumpButtonSprite = new Sprite("sprites/jump.png", context, "jump");
		leftButtonSpriteP = new Sprite("sprites/left_p.png", context, "leftP");
		rightButtonSpriteP = new Sprite("sprites/right_p.png", context, "rightP");
		shootButtonSpriteP = new Sprite("sprites/shoot_p.png", context, "shootP");
		jumpButtonSpriteP = new Sprite("sprites/jump_p.png", context, "jumpP");
	}
	
}
